#define _CRT_SECURE_NO_WARNINGS 1
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>

typedef struct  BinaryTree* QDataType;
typedef struct QueueNode
{
	struct QueueNode* next;
	QDataType data;
}QNode;

typedef struct Queue
{
	QNode* phead;
	QNode* ptail;
	int size;
}Queue;

void QInit(Queue* pq);
void QPush(Queue* pq, QDataType x);
void QDestory(Queue* pq);
void QPop(Queue* pq);
QDataType QBack(Queue* pq);
QDataType QFront(Queue* pq);
int QSize(Queue* pq);
bool QEmpty(Queue* pq);

void QInit(Queue* pq)
{
	assert(pq);

	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}

void QDestory(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->phead;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->phead = NULL;
	pq->ptail = NULL;
	pq->size = 0;
}

void QPush(Queue* pq, QDataType x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	newnode->data = x;
	newnode->next = NULL;
	if (pq->ptail == NULL)
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	pq->size++;
}

void QPop(Queue* pq)
{
	assert(pq);
	assert(!QEmpty(pq));
	if (pq->phead->next == NULL)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		QNode* cur = pq->phead->next;
		free(pq->phead);
		pq->phead = cur;
	}
	pq->size--;
}

QDataType QBack(Queue* pq)
{
	assert(pq);
	assert(!QEmpty(pq));
	return pq->ptail->data;
}

QDataType QFront(Queue* pq)
{
	assert(pq);
	assert(!QEmpty(pq));
	return pq->phead->data;
}

int QSize(Queue* pq)
{
	return pq->size;
}

bool QEmpty(Queue* pq)
{
	assert(pq);
	if (pq->size == 0)
		return true;
	return false;
}